<?php
session_start( );
?>
<?php
require_once('connections/mysqlDB.php');
require('smarty_connect.php');
$smarty = new smarty_connect;
$dbConn = new mysqli(HOST,USER,PWD,DB);
if (mysqli_connect_errno()) { die('Connect failed: ' . mysqli_connect_error()); }  

$dbConn->query("SET NAMES 'utf8'");

function rcount ($array, $limit)
{
    foreach ($array as $id => $_array)
    {
        if (is_array ($_array) && $limit > 0) $count += count_recursive ($_array, $limit - 1); else $count += 1;
    }
    return $count;
}

// regrouping registration request variables into array
for ($n = 0; $n < 6; $n++) {
   $iv[$n]["cname$n"]  = $_POST["cname$n"];
   $iv[$n]["firstname$n"]  = $_POST["firstname$n"];
   $iv[$n]["lastname$n"] = $_POST["lastname$n"];
   $iv[$n]["gender$n"] = $_POST["gender$n"];
   $iv[$n]["relationship$n"] = $_POST["relationship$n"];
   $iv[$n]["group$n"] = $_POST["group$n"];
   $iv[$n]["spneed$n"] = $_POST["spneed$n"];
   
   // meals plan
   for ($m=1; $m < 9; $m++) {
   if ($_POST["meal$n$m"] == 'checkbox') {
       $iv[$n]["meal$n"]["$m"] = 1;
     } else {  $iv[$n]["meal$n"]["$m"] = 0;  }
   }
   // section to attend
    for ($m=1; $m < 12; $m++) { 
    if ($_POST["session$n$m"] == 'checkbox') {
       $iv[$n]["session$n"]["$m"] = 1;
     } else {  $iv[$n]["session$n"]["$m"] = 0;  } 
   }  
}

/*
var_dump($iv);
print_r($_REQUEST);
*/

if ($_POST['country'] == "USA") $_SESSION['foreign'] = "N"; else $_SESSION['foreign'] = "Y" ;

if (isset($_POST['pastor'])){
	$pastor=$_POST["pastor"];
} else {$pastor="N"; }

$_SESSION['pastor'] = $pastor;
$conferenceid =$_POST["conferenceID"];

// check whether the registrant is adding for other people
if (isset($_POST['regid'])) {
	$regid=$_POST['regid'];
	} else { $regid=0;
		   }

// check if this is for first-time comer

if (isset($_POST['firsttime'])) {$firsttime="Y";
} else {$firsttime="N"; }

// check email already exist or not

if (!isset($_SESSION['verifyUser'])) {
$email=$_POST['email'];
//echo "email=$email<br>";
//var_dump($_REQUEST);
$stmt = $dbConn->query("select email from registrant where email = '$email'");
$cnt = $stmt->num_rows;
$stmt->Close();
//echo "row=$cnt";
//echo "regid=$regid";

if ($cnt >=1 ) {
$smarty->assign('error',"重複email或者您已完成特會註冊， email already exist, please use another one or login.");
$smarty->display('error.tpl');
}
}

//
if ($cnt <= 0 ) {

// if we already have a $regid then we don't need to create another registrant's profile
if ($regid == 0) {
 if ($stmt1 = $dbConn->prepare("INSERT INTO registrant (cname, first_name, last_name,pastoral_staff,gender, home_phone,mobile_phone,work_phone,email,pwd,address1,address2,city,state,zip,country,cmid,church,title,church_size,church_size_other,create_date) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())")) {
    $stmt1->bind_param('sssssssssssssssssssss',$_POST['cname'], $_POST['firstname'],$_POST['lastname'], $pastor, 
	  $_POST['gender'],$_POST['home_phone'],$_POST['cell_phone'],$_POST['work_phone'],$_POST['email'],
	  $_POST['password'],$_POST['address1'],$_POST['address2'],$_POST['city'],$_POST['state'],
	  $_POST['zipcode'],$_POST['country'],$_POST['church'],$_POST['other_church'],$_POST['title'],
	  $_POST['churchsize'],$_POST['size_other']);
    $stmt1->execute();
    //printf ("Added records: %d<br>", $stmt->affected_rows);
	$regid=$stmt1->insert_id;
	$_SESSION['regid']=$regid;
	if (trim($_POST['cname']) <> "") {
		$_SESSION['fullname'] = $_POST['cname'];
	} else { $_SESSION['fullname']=trim($_POST['firstname']). " " . trim($_POST['lastname']);
	}
	//printf ("Registrant New Record has id %d.\n", $stmt->insert_id);
  }
}
  $stmt1->close();
  
 // insert into registration
     if ($stmt2 = $dbConn->prepare("INSERT INTO registration (regid,conference_id,create_date) VALUES (?,?,NOW())")) {
    $stmt2->bind_param('is', $regid, $conferenceid);
    $stmt2->execute();
    //printf ("<br>Added records: %d<br>", $stmt->affected_rows);
	$rid=$stmt2->insert_id;
	$_SESSION['rid']=$rid;
	//printf ("registration New Record has id %d.\n", $stmt->insert_id);
  }

  $stmt2->close();
 // insert into registration_info
 
 //echo "<br>Names:<br>";
for ($n = 0; $n < 6; $n++) {

if ( ($iv[$n]["firstname$n"]) & ($iv[$n]["lastname$n"]) ) {
 
if ( ($iv[$n]["firstname$n"] == $_POST['firstname']) && ( $iv[$n]["cname$n"] == $_POST['cname']) ){
 $ps=$pastor;
} else { $ps="N"; }

//echo "<br>" . $iv[$n]["lastname$n"] . "," . $iv[$n]["firstname$n"] . '-' . $ps . "<br>"; 

$sql = "Insert into registration_info (rid, cname, first_name, last_name, first_time, pastoral_staff,relationship,gender,groups,special_need,
meal1,meal2,meal3,meal4,meal5,meal6,meal7,meal8,
session1,session2,session3,session4,session5,session6,session7,session8,
session9,session10,session11,create_date ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())";

  if ($stmt3 = $dbConn->prepare($sql)) {
    $stmt3->bind_param('isssssssssiiiiiiiiiiiiiiiiiii',$rid, $iv[$n]["cname$n"], $iv[$n]["firstname$n"], $iv[$n]["lastname$n"], $firsttime, $ps,
	$iv[$n]["relationship$n"],$iv[$n]["gender$n"],$iv[$n]["group$n"],$iv[$n]["spneed$n"],
	$iv[$n]["meal$n"][1],$iv[$n]["meal$n"][2],$iv[$n]["meal$n"][3],$iv[$n]["meal$n"][4],
	$iv[$n]["meal$n"][5],$iv[$n]["meal$n"][6],$iv[$n]["meal$n"][7],$iv[$n]["meal$n"][8],
	$iv[$n]["session$n"][1],$iv[$n]["session$n"][2],$iv[$n]["session$n"][3],$iv[$n]["session$n"][4],
	$iv[$n]["session$n"][5],$iv[$n]["session$n"][6],$iv[$n]["session$n"][7],$iv[$n]["session$n"][8],
	$iv[$n]["session$n"][9],$iv[$n]["session$n"][10],$iv[$n]["session$n"][11]);
 $stmt3->execute();
 //printf ("registration_info New Record has id %d.\n", $stmt1->insert_id);

 }
}
} 
$cnt =$stmt3->affected_rows;
$stmt3->close();
$dbConn->close();

if($cnt >=1){
  header("location:con_nextpage.php");
}

}

?>
